home *** CD-ROM | disk | FTP | other *** search
- Path: slsyd1p17.ozemail.com.au!user
- From: conor@cognet.com.au (Conor MacNeill)
- Newsgroups: comp.lang.c++
- Subject: Re: char vs. unsigned char ???
- Date: Tue, 23 Jan 1996 00:32:50 +1100
- Organization: Cognet Pty Limited
- Message-ID: <conor-2301960032500001@slsyd1p17.ozemail.com.au>
- References: <DLGEKq.1zq@avalon.chinalake.navy.mil>
- NNTP-Posting-Host: slsyd1p17.ozemail.com.au
- X-Newsreader: Yet Another NewsWatcher 2.1.2
-
- In article <DLGEKq.1zq@avalon.chinalake.navy.mil>, sean harvey
- <charles_harvey@imdgw.chinalake.navy.mil> wrote:
-
- >I recently encorporated some functions from another program compiled and
- >linked under an older version (Microsoft C version 6.0) into my program
- >(MSVC 7.0).
- >
- >The alien code would not compile for me due, I guess to more rigorous
- >type-checking.
- >
- >He used a lot of unsigned char strings that generated compile errors on
- >calls to functions strlen, strcat and sprintf.
- >
- >So I changed his types to char and compiled cleanly but the program
- >crashes and I think it may be due to the changes.
- >
- >The functions pass the data around and compress it to 6-bit ascii, among
- >other things. Then I send it out via a modem.
- >
- >Somebody splain to me about unsigned char vs char please. Or any other
- >input.
-
- Both char and unsigned char are 8 bits in most implementations. The difference
- is in how to interpret those bits.
-
- An unsigned char can take the value 0 to 255
-
- A signed char can take the values -128 to 127.
-
- so the binary value 11111111 is -1 in a signed char but 255 in an
- unsigned char (same bits - different values)
-
- Some compilers treat chars as signed, some treat them as unsigned and
- some compilers are configurable.
-
- An important difference between signed and unsigned chars occurs
- when they are converted to larger integral types such as ints and longs
- Signed characters are sign-extended to become larger types while
- unsigned chars are not.
-
- A problem may be occuring if your alien code is using the character value
- to index into an array. A unsigned char will work nicely while a signed char
- may attempt to access memory outside the array (since the index may become
- negative). Without more info, this is a wild guess at your problem :-)
-
- Good Luck
-
- --
- Conor MacNeill
- conor@cognet.com.au
- Cognet Pty Limited
-